#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++) 
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-', ch = getchar(); 
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x;
}

const int N = 1010;
const int M = 1e4 + 10;
const int L = 3e5 + 10;

int n, m, K, ans[L];
int fa[M][N];
struct edge { int u, v, id, w; } e[L];

int gf(int *fa, int x) { return fa[x] == x ? x : fa[x] = gf(fa, fa[x]); }

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in, K = in; rep(i, 1, K) rep(j, 1, n) fa[i][j] = j;
	rep(i, 1, m) e[i].u = in, e[i].v = in, e[i].w = in, e[i].id = i;
	sort(e + 1, e + m + 1, [](auto x, auto y) { return x.w > y.w; });
	rep(i, 1, m) {
		int l = 1, r = K, res = 0;
		while(l <= r) {
			int mid = l + r >> 1;
			if(gf(fa[mid], e[i].u) != gf(fa[mid], e[i].v)) res = mid, r = mid - 1;
			else l = mid + 1; 
		}
		ans[e[i].id] = res;
		if(res) fa[res][gf(fa[res], e[i].u)] = fa[res][gf(fa[res], e[i].v)];
	}
	rep(i, 1, m) printf("%d\n", ans[i]);
	return 0;
}
